//
//  MyCell.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/11/18.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

//代理
protocol MyCellDelegate {
    ///跳转我的积分界面
    func jumpToAccout()
    
    ///跳转我的钱包
    func jumpToCash()
    
    ///跳转头像
    func selePhoto()
}

class MyCell: UITableViewCell {
    
    static let shared = MyCell()
    //设置代理监听按钮事件
    var delegate : MyCellDelegate?
    
    
    //========================================================================
    //第一组第一行
    //========================================================================
    //背景图片
    lazy var bgImg : UIImageView = {
        let img = UIImageView()
        img.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 150)
        img.backgroundColor = UIColor.blue
        return img
    }()

    //头像
    lazy var imgView : UIImageView = {
        let img = UIImageView()
        img.backgroundColor = UIColor.orange
//        img.layer.cornerRadius = 25
        img.frame = CGRect(x: UIScreen.main.bounds.width * 0.5 - 25, y: self.bounds.height * 0.5 + 12.5, width: 50, height: 50)
        img.layer.borderColor = UIColor.white.cgColor
        img.layer.borderWidth = 1
        img.isUserInteractionEnabled = true
        
        //单机手势，使其可以跳转页面
        let tapGes = UITapGestureRecognizer.init(target: self, action: #selector(MyCell.clickImgSEL))
        img.addGestureRecognizer(tapGes)
        return img
    }()
    
    //名字
    lazy var nameLabel : UILabel = {
        let lab = UILabel()
          lab.frame = CGRect(x:0, y: self.bounds.height * 0.5 + 12.5 + 50 + 10, width:UIScreen.main.bounds.width , height: 25)
        lab.text = "ddd"
        lab.textAlignment = NSTextAlignment.center
        return lab
    }()
    
    
    //========================================================================
    //第二组
    //========================================================================
    //tf的文字
    var downNameLabel: UILabel = {
        let lab = UILabel()
        lab.frame = CGRect(x: 50, y: 0, width: UIScreen.main.bounds.size.width - 60 , height: 45)
//        lab.placeholder = "ddd"
        //文字左对齐
        lab.textAlignment = NSTextAlignment.left
        lab.font = UIFont.systemFont(ofSize: 14)
        return lab
    }()
    
    //前置图片
    lazy var qianzhiImg: UIImageView = {
        let img = UIImageView()
        //自适应放大
        img.contentMode = UIViewContentMode.scaleAspectFit
        img.frame = CGRect(x: 7.5, y: 7.5, width: 30, height: 30)
        return img
    }()
    
    //分割线
    lazy var line : UIView = {
        let lin = UIView()
        lin.frame = CGRect(x: 0, y: 45, width: UIScreen.main.bounds.size.width, height: 0.5)
        lin.backgroundColor = UIColor.lightGray
        return lin
    }()
    
    //尖角
    lazy var disclosureImg : UIImageView = {
        let img = UIImageView()
        
        img.contentMode = UIViewContentMode.scaleAspectFit
        img.frame = CGRect(x: UIScreen.main.bounds.width - 30, y: 7.5, width: 15, height: 30)
        img.backgroundColor = UIColor.black
        return img
    }()
    
    //========================================================================
    //第一组第二行
    //========================================================================
    //前面的view
    lazy var oneViewFrontView : UIView = {
        let view = UIView()
        view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width * 0.6, height: 55)
        view.backgroundColor = UIColor.brown
        return view
    }()
    
    //后面的view
    lazy var oneViewBackView : UIView = {
        let view = UIView()
        view.frame = CGRect(x:  UIScreen.main.bounds.width * 0.6, y: 0, width: UIScreen.main.bounds.width * 0.4, height: 55)
        view.backgroundColor = UIColor.purple
        return view
    }()
    
    //前面view中画一个居中的view，将控件塞进去
    lazy var frontMiddleView : UIView = {
        let view = UIView()
        
        view.backgroundColor = UIColor.white
        return view
    }()
    
    //后面view中画一个居中的view，将控件塞进去
    lazy var backMiddleView : UIView = {
        let view = UIView()
        
        view.backgroundColor = UIColor.white
        return view
    }()
    
    
    //========================================================================
    //开始塞控件
    //========================================================================
    //前面的view
    
    lazy var seperateImg : UIImageView = {
        let img = UIImageView()
        img.backgroundColor = UIColor.red
        return img
    }()
    
    //积分图标
    lazy var jifenImg : UIImageView = {
        let img = UIImageView()
        img.image = UIImage.init(named: "money")
//        img.backgroundColor = UIColor.black
        return img
    }()
    
    //积分
    lazy var jifenLabel : UILabel = {
        let lab = UILabel()
//        lab.text = "6789"
        lab.textColor = UIColor.green
        lab.textAlignment = NSTextAlignment.center
        return lab
    }()
    
    //我的积分
    lazy var myAccount : UILabel = {
        let lab = UILabel()
        lab.text = "我的积分"
        lab.textAlignment = NSTextAlignment.center
        return lab
    }()
    
    //========================================================================
    //开始塞控件
    //========================================================================
    //后面的view

    //钱包图标
    lazy var cashImg : UIImageView = {
        let img = UIImageView()
        
        img.backgroundColor = UIColor.black
        return img
    }()
    
    //充值
    lazy var cashLabel : UILabel = {
        let lab = UILabel()
//        lab.text = "充值"
        lab.text = "提现"
        lab.textColor = UIColor.green
        lab.textAlignment = NSTextAlignment.center
        return lab
    }()
    
    
    //========================================================================
    //第一组第二行 添加按钮单击事件(前面)
    //========================================================================
    lazy var frontBtn : UIButton = {
        let btn = UIButton()
//        btn.backgroundColor = UIColor.lightGray
        //frontBtnSEL
        btn.addTarget(self, action: #selector(MyCell.frontBtnSEL), for: .touchUpInside)
        return btn
    }()

    //第一组第二行 添加按钮单击事件(后面)
    lazy var backBtn : UIButton = {
        let btn = UIButton()
//        btn.backgroundColor = UIColor.red
        btn.addTarget(self, action: #selector(MyCell.backBtnSEL), for: .touchUpInside)
        return btn
    }()
    

    //重写构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        
        //第一组，第二行前面视图居中部分
        frontMiddleView.frame = CGRect(x: oneViewFrontView.frame.size.width - oneViewFrontView.frame.size.width * 0.825 , y: 0, width: oneViewFrontView.frame.size.width * 0.65, height: 55)
        
        //第一组，第二行后面视图居中部分
        backMiddleView.frame = CGRect(x: oneViewBackView.frame.size.width - oneViewBackView.frame.size.width * 0.825 , y: 0, width: oneViewBackView.frame.size.width * 0.65, height: 55)
        
        //第一组，第二行前面视图分开的线条
        seperateImg.frame = CGRect(x: oneViewFrontView.frame.size.width - 3, y: 0, width: 3, height: 55)
        
        //第一组，第二行前面视图积分图片
        jifenImg.frame = CGRect(x: oneViewFrontView.frame.size.width * 0.2, y: 15, width: 20, height: 20)
        
        //第一组，第二行前面视图积分分数文字
        jifenLabel.frame = CGRect(x: oneViewFrontView.frame.size.width * 0.2 + 20 + 10, y: 5, width: oneViewFrontView.frame.size.width * 0.4, height: 20)
        
        //第一组，第二行前面视图积分- 我的积分
        myAccount.frame = CGRect(x: oneViewFrontView.frame.size.width * 0.2 + 20 + 10, y: 30, width: oneViewFrontView.frame.size.width * 0.4, height: 25)
        
        //第一组，第二行后面视图积分- 钱包图片
        cashImg.frame = CGRect(x: backMiddleView.frame.size.width * 0.1, y: 15, width: 20, height: 20)
        
        //第一组，第二行后面视图积分- 钱包文字
        cashLabel.frame = CGRect(x: backMiddleView.frame.size.width * 0.2 + 20, y: 15, width: 40, height: 20)
        
        //第一组，第二行添加按钮（前面的）
        frontBtn.frame = oneViewFrontView.frame

        //第一组，第二行添加按钮（后面的）
        backBtn.frame = backMiddleView.frame
//        backBtn.frame = CGRect(x: 0, y: 0, width: 100, height: 100)

        
        addSubview(bgImg)
        addSubview(nameLabel)
        addSubview(downNameLabel)
        addSubview(qianzhiImg)
        addSubview(line)
        addSubview(disclosureImg)
        addSubview(imgView)

        //第一组第二行
        addSubview(oneViewFrontView)
        addSubview(oneViewBackView)
    
        
        
        //第一组第二行前面的控件
        oneViewFrontView.addSubview(frontMiddleView)
        oneViewFrontView.addSubview(seperateImg)
        oneViewFrontView.addSubview(jifenImg)
        oneViewFrontView.addSubview(jifenLabel)
        oneViewFrontView.addSubview(myAccount)
        oneViewFrontView.addSubview(frontBtn)
//        oneViewBackView.addSubview(backBtn)
        
        //添加奇怪的分割线
        oneViewBackView.addSubview(backMiddleView)
        
        //第一组第二行后面的控件
        backMiddleView.addSubview(cashImg)
        backMiddleView.addSubview(cashLabel)
        oneViewBackView.addSubview(backBtn)

        //去除高亮效果
        self.selectionStyle = .none
        
        //取出本地个人信息数据
        guard let peronData = localSave.object(forKey: personInfo) as? NSDictionary else {
            return
        }
        
        let imgURL = peronData["img"] as! String
        
        do {
            imgView.image = try UIImage.init(data: Data.init(contentsOf: URL.init(string: imgURL)!))
        } catch  {
            
        }
        
        nameLabel.text = peronData["nickname"] as? String
        
        print("积分",peronData["integral"] as? String)
        
        jifenLabel.text = peronData["integral"] as? String
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")

    }
   
}

// MARK:- 单击事件
extension MyCell {
    func clickImgSEL() -> Void {
        
    }
    
    //第一组第二行 前面按钮单机事件
    func frontBtnSEL() -> Void {
        print("前面的按钮单击事件")
        delegate?.jumpToAccout()
    }
    //第一组第二行 后面按钮单机事件
    func backBtnSEL() -> Void {
        print("后面的按钮单击事件")
        delegate?.jumpToCash()
    }
    
    
}


